package com.vcredit.creditcat.web.api;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.ICrfManager;
import com.vcredit.creditcat.api.IXyhManager;
import com.vcredit.creditcat.api.IShanDaiManager;
import com.vcredit.creditcat.api.IProductApplyManager;
import com.vcredit.creditcat.dto.product.CreditCRFAmountParamDto;
import com.vcredit.creditcat.enumtype.ApiEnum;
import com.vcredit.creditcat.exception.DataException;
import com.vcredit.creditcat.util.EnumExtUtil;
import com.vcredit.creditcat.util.JsonUtil;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.creditcat.vo.CRFResult;
import com.vcredit.framework.annotation.InterfaceRequest;
import com.vcredit.framework.annotation.Log;

@RestController
@RequestMapping("/product")
public class ThirdPartyProductController extends BaseCreditCatController {
	
	private static final Logger LOG = LoggerFactory.getLogger(ThirdPartyProductController.class);

	@Reference(version = "1.0")
	private IProductApplyManager productApplyManager;

	@Reference(version = "1.0")
	private ICrfManager iCrfManager;
	
	@Reference(version = "1.0")
	private IXyhManager IXyhManager;
	
	@Reference(version = "1.0")
	private IShanDaiManager iShanDaiManager;

	private final String requestSucce = "反馈成功！";
	private final String containsErr = "参数包含特殊字符！";
	private final String systemErr = "处理结果出现异常！";

	@Log
	@InterfaceRequest("额度申请状态推送")
	@RequestMapping(value = "/{product}/orderResult")
	public CRFResult<?> noticeResult(@PathVariable("product") String productName, HttpServletRequest request,
			@RequestBody CreditCRFAmountParamDto jsonParam) throws Exception {
		String jsonEntry = "";
		try {

			ApiEnum enumOnValue = EnumExtUtil.getEnumOnValue(ApiEnum.class, productName, "key");
			switch (enumOnValue) {
			case crfProduct:
				// 解密
				// 获取头部验签
				String signature = request.getHeader("X-CrfChina-Signature");
				String datasou = request.getHeader("X-Data-Source");
				jsonEntry = iCrfManager.decodeJsonParampub(signature, datasou, jsonParam);
				break;
			case creditFlower:
				jsonEntry = JsonUtil.obj2Json(jsonParam, true);
				
				LOG.info("------------信用花授信回调参数：" + jsonEntry);
				
				IXyhManager.dealResult(productName, jsonEntry);
				return CRFResult.SuccessResponse(requestSucce);	
			case shanDai:
				jsonEntry = JsonUtil.obj2Json(jsonParam, true);
				
				LOG.info("------------闪贷授信回调参数：" + jsonEntry);
				
				iShanDaiManager.dealResult(productName, jsonEntry);
				return CRFResult.SuccessResponse(requestSucce);	
			default:
				break;
			}
			
			// 特殊字符防注入
			boolean flag = Utility.booleanescapeExprSpecialWord(jsonEntry);
			if (flag) {
				iCrfManager.dealResult(productName, jsonEntry);
				return CRFResult.SuccessResponse(requestSucce);
			} else {
				return CRFResult.ErrorResponse(containsErr);
			}
		} catch (DataException e) {
			return CRFResult.ErrorResponse(e.getMessage());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return CRFResult.ErrorResponse(systemErr);
		}
	}

}
